#ifndef __COMM_HPP__//防止这个头文件被重复包含，第一次包含时，
#define __COMM_HPP__//__COMM_HPP__,没有被定义，执行下下面代码，当第二次包含时， 
                    //__COMM_HPP__已经被定义，直接跳转到#endif，避免了这个头文件被重复包含。
#include<iostream>
#include<string>
#include<cstring>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<cassert>
using namespace std;

#define Mode 0666
#define Path "./fifo"
class Fifo
{
    public:
    Fifo(const string& path = Path):_path(path)
    {
        umask(0);//将文件掩码设置为0
        int n = mkfifo(_path.c_str(), Mode);
        if(n == 0)
        {
            cout<<"mkfifo success!"<<endl;
        }
        else
        {
            cout<<"mkfifo failed ,error:"<<errno<<", errstring: "<< strerror(errno) <<endl;
        }
    }
    ~Fifo()
    {
        int n = unlink(_path.c_str());
        if(n == 0)
        {
            cout<<"unlink success!"<<endl;
        }
        else
        {
            cout<<"unlink failed ,error:"<<errno<<", errstring: "<< strerror(errno) <<endl;
        }
    }
    private:
    string _path;//文件路径+名字
};


class syns
{
    public:
        syns():_wfd(-1),_rfd(-1)
        {

        }
        void OpenReadOrDie()
        {
            _rfd = open(Path,O_RDONLY);
            if(_rfd < 0)
            {
                cout<<"退出程序"<<endl;

                exit(1);
            }
        }
        void OpenWriteOrDie()
        {
            _wfd = open(Path,O_WRONLY);
            if(_wfd < 0)
            {
                cout<<"退出程序"<<endl;
                exit(1);
            }
        }
        bool wait()
        {
            bool ret = true;
            uint32_t c = 0;
            ssize_t n = read(_rfd,&c,sizeof(uint32_t));
            if(n == sizeof(uint32_t))
            {
                std::cout << "server wakeup, begin read shm..." << std::endl;
            }
            else if(n == 0)
            {
                return false;
            }
            else
            {
                return false;
            }
            return ret;
        }
        void wakeup()
        {
            uint32_t c = 0;
            ssize_t n = write(_wfd,&c,sizeof(c));
            assert(n == sizeof(uint32_t));

            std::cout << "wakeup server..." << std::endl;
        }
        ~syns()
        {}
    private:
        int _wfd;
        int _rfd;
};


#endif